package com.sun.mybatisplus.redis.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * 分布式锁redission的配置
 */
@Configuration
public class RedissonConfiguration {

    /**
     * ***********************************************************
     * 或者，在 application.yml 中配置如下:
     * spring:
     * redis:
     * redisson:
     * config: classpath:redisson-config.yaml
     * ***********************************************************
     *
     * @return
     * @throws IOException
     */
    @Bean
    public RedissonClient redisson() throws IOException {
        // 两种读取方式，Config.fromYAML 和 Config.fromJSON
        // Config config = Config.fromJSON(RedissonConfiguration.class.getClassLoader().getResource("redisson-config.json"));
        Config config = Config.fromYAML(RedissonConfiguration.class.getClassLoader().getResource("redisson-config.yml"));
        return (Redisson) Redisson.create(config);

        //或者这样写
//        RedissonClient redisson = Redisson.create(
//                Config.fromYAML(new ClassPathResource("redisson-config.yml").getInputStream()));
//        return  redisson;
    }
}
